var ProjectDetail = {};
ProjectDetail.projectId = "";

ProjectDetail.contextpath = "";
ProjectDetail.initPage = function(contextpath, projectId) {
	ProjectDetail.contextpath = contextpath;
	ProjectDetail.projectId = projectId;
	$(document).on("keyup", "#keyword", function() {
		var keyword=$("#keyword").val();
		layui.use(['table','form'], function() {
			var table = layui.table;
			var form = layui.form;
		table.reload('projectDetail', {
			where : {
				"projectId" : ProjectDetail.projectId,
				"keyword":keyword
			},
		});
		});
		$("#keyword").val(keyword);
		$("#keyword").focus();
	});
	$(document).on("keyup", "#keyword2", function() {
		var keyword=$("#keyword2").val();
		layui.use(['table','form'], function() {
			var table = layui.table;
			var form = layui.form;
		table.reload('paramsDetail', {
			where : {
				"interfaceId" : ProjectDetail.interfaceId,
				"keyword":keyword
			},
		});
		});
		$("#keyword2").val(keyword);
		$("#keyword2").focus();
	});
	layui.use(['table','form'], function() {
		var table = layui.table;
		var form = layui.form;
		// 第一个实例
		table.render({
			id : 'projectDetail',
			elem : '#interface_table',
			toolbar : '#interface_bar',
			height: 300,
//			width: 892,
			url : '/web2019/project/ajaxQueryProjectInterface.action', // 数据接口
			where : {
				"projectId" : projectId,
				"keyWord":$("#keyword").val()
			},
			page : false,// 开启分页
			response : {
				statusName : 'code' // 规定数据状态的字段名称，默认：code
				,
				statusCode : "0000" // 规定成功的状态码，默认：0
				,
				msgName : 'message' // 规定状态信息的字段名称，默认：msg
				,
				countName : 'total' // 规定数据总数的字段名称，默认：count
			},
			cols : [ [ // 表头
			{
				field : 'interfaceId',
				title : '接口id',
				width : 100,
				sort : true
			}, {
				field : 'interfaceCode',
				title : '接口编码',
				edit : 'text',
				width : 250
			}, {
				field : 'interfaceName',
				title : '接口名称',
				edit : 'text',
				width : 250,
				sort : true
			}, {
				field : 'interfaceType',
				title : '接口类型',
				edit : 'text',
				width : 100,
				sort : true
			}, {
				field : 'description',
				title : '描述',
				edit : 'text',
				width : 300,
				sort : true
			}, 
			{
				field : 'interfacePath',
				title : '接口路径',
				edit : 'text',
				width : 200,
				sort : true
			}
			, 
			{
				field : 'inputInterfaceName',
				title : '入参接口名称',
				edit : 'text',
				width : 200,
				sort : true
			}, {
				field : 'outputInterfaceName',
				title : '出参接口名称',
				edit : 'text',
				width : 200,
				sort : true
			}, {
				field : 'showOrder',
				title : '排序',
				edit : 'text',
				width : 100,
				sort : true
			},
			{
				title : '操作',
				templet : function(item) {
					return ProjectDetail.setOperate(item, projectId);
				},
				width : 500
				,
				align : 'center',
				fixed : 'right'
			} ] ]
		});
		// 监听单元格编辑
		table.on('tool(interface-filter)', function(obj) {
//			console.log(obj);
			if(obj.event == 'delete'){
		      layer.confirm('真的删除行么', function(index){
		        obj.del();
		        ProjectDetail.deleteInterface(obj.data);
		        layer.close(index);
		      });
		    } else if(obj.event == 'add'){
		    	ProjectDetail.doAdd(obj.data,projectId)
		    }else if(obj.event == 'update'){
		    	ProjectDetail.doInterfaceUpdate(obj.data,projectId)
		    }else if(obj.event == 'watch'){
		    	ProjectDetail.interfaceId=obj.data.interfaceId;
		    	ProjectDetail.doSelect(obj.data.interfaceId)
		    }else if(obj.event == 'create'){//生成入参报文
		    	ProjectDetail.getInputParam(obj.data.interfaceId)
		    }else if(obj.event == 'tree'){//生成树形结构
		    	ProjectDetail.getParamTree(obj.data.interfaceId)
		    }else if(obj.event == 'addTest'){//生成树形结构
		    	ProjectDetail.addTest(obj.data.interfaceId);
		    }
		});
		
		layui.form.render('select');

	});
}
ProjectDetail.setOperate = function(data, projectId) {
	var interfaceId = data.interfaceId
	var btnHtml = "";
	if (data.interfaceId != null) {
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs" lay-event="watch" >查看</button>';
	}
	if (data.interfaceId != null) {
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs" lay-event="create" >生成入参报文</button>';
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs" lay-event="tree" >数形结构</button>';
	}
	if (data.interfaceId != null) {
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs" lay-event="update">更新</button>';
	} else {
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs" lay-event="add" >添加</button>';
	}
	if (data.interfaceId != null) {
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</button>';
//		btnHtml += '<button type="button" class="layui-btn layui-btn-xs" lay-event="addTest">添加测试用例</button>';
	}
	
	return btnHtml;
}

/**
 * 添加项目接口
 */
ProjectDetail.doAdd = function(data,projectId) {
	if(ProjectDetail.checkAdd(data)){
		data.projectId =projectId;
		ProjectDetail.ajaxSaveInterface(data);
	}
}
/**
 * 添加测试用例
 */
ProjectDetail.addTest =function(interfaceId){
	
	
}
ProjectDetail.getFormat =function(str,type){
	Common.callClient("/web2019/common/getFormatString.action",{"str":str,"type":type},function(data){
		if (data.code == "0000") {
			$("#context").html(data.str);
		} else {
				layer.alert(data.message);
		}
	});
}


ProjectDetail.getInputParam = function(interfaceId) {
	var data ={"interfaceId":interfaceId};
	Common.callClient("/web2019/project/getInputParam.action",data,function(data){
		if (data.code == "0000") {
			layer.alert(data.input);
		} else {
			layer.alert(data.message);
		}
	});
}

ProjectDetail.getParamTree = function(interfaceId) {
	var input ={"interfaceId":interfaceId};
	Common.callClient("/web2019/project/getParamTree.action",input,function(data){
			if (data.code == "0000") {
				ProjectDetail.openParamTree(data);
			} else {
				layer.alert(data.message);
			}
	});
}
ProjectDetail.openParamTree =function(data){
    layer.open({
        type: 1
        ,title: '树形结构'
        ,area: ['600px', '600px']
        ,shade: 0
        ,maxmin: true
        ,content: ProjectDetail.getHtml()
        ,btn: ['关闭']
        ,yes: function(){
    		  layer.closeAll();
        }
        ,success: function(layero){
        	layui.use(['tree'], function(){
        		var tree = layui.tree
        		  tree.render({
        			    elem: '#test5' //默认是点击节点可进行收缩
        			    ,data: data.data
        			  });
        		});
        }
      });
}
ProjectDetail.getHtml= function(){
	var html="";
	html+='<div id="test5" class="demo-transfer"style="width:500px;height:400px;">';
	html+='</div>';
	return html;
}

ProjectDetail.checkAdd = function(data) {
	if(data.interfaceCode==null||data.interfaceCode==""){
		layer.msg("请输入接口编号");
		return false;
	}
	if(data.interfaceName==null||data.interfaceName==""){
		layer.msg("请输入接口名称");
		return false;
	}
	return true;
}
ProjectDetail.ajaxSaveInterface =function(data){
	Common.callClient("/web2019/project/ajaxSaveInterface.action",data,function(data){
		if (data.code == "0000") {
			layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
				layer.close(index);
				layui.use(['table'], function() {
					var table = layui.table;
				table.reload('projectDetail', {
					where : {
						"projectId" : ProjectDetail.projectId
					},
			    });
				});
				
			});
		} else {
			layer.alert(data.message);
		}
	});
}


ProjectDetail.deleteInterface =function(data1){
	Common.callClient("/web2019/project/ajaxDeleteInterface.action",data1,function(data){
		if (data.code == "0000") {
			layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
				layer.close(index);
				layui.use(['table'], function() {
					var table = layui.table;
				table.reload('paramsDetail', {
					where : {
						"interfaceId" : data1.interfaceId
					},
			    });
				});
			});
		} else {
			layer.alert(data.message);
		}
	});
	
}

ProjectDetail.getData= function(obj){
	var item1 =obj.tr[0].childNodes[3];
	var paramType =$(item1).find("select").val();
	var item2 =obj.tr[0].childNodes[6];
	var paramDataType =$(item2).find("select").val();
	var item3 =obj.tr[0].childNodes[8].textContent;
	var isNotNull ='0';
	if(item3.trim()=="是"){
		isNotNull ='1';
	}
	var item4 =obj.tr[0].childNodes[10].textContent;
	var paramDirection ='0';
	if(item4.trim()=="出参"){
		paramDirection ='1';
	}
	obj.data.paramType =paramType;
	obj.data.paramDataType =paramDataType;
	obj.data.isNotNull =isNotNull;
	obj.data.paramDirection =paramDirection;
	return obj.data;
}
ProjectDetail.doInterfaceUpdate = function(data,projectId) {
	if(ProjectDetail.checkAdd(data)){
		data.projectId =projectId;
		ProjectDetail.ajaxSaveInterface(data);
	}
}